package com.hf.eagle.web.struts.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hf.eagle.domain.SaleGrid;
import com.hf.eagle.domain.SysModule;
import com.hf.eagle.domain.UserInfo;
import com.hf.eagle.utils.StringHelper;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.Keys;

/**
 * @author Xu,XiaoYuan
 * @version 2011.8.16 23:45
 */
public class FramesAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.index(mapping, form, request, response);
	}

	public ActionForward index(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return mapping.findForward("index");
	}

	public ActionForward leftFrame(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		UserInfo userInfo = (UserInfo) session.getAttribute(Keys.ADMIN_USER);
		if (null == userInfo) {
			String contextPath = request.getContextPath();
			if (contextPath.endsWith("/")) {
				contextPath = contextPath.substring(0, contextPath.length() - 1);
			}
			response.sendRedirect(contextPath + "/timeout.jsp");
			return null;
		}

		SaleGrid saleGrid = new SaleGrid();
		saleGrid.setIs_del(0);
		List<SaleGrid> saleGridList = super.getFacade().getSaleGridService().getSaleGridList(saleGrid);
		request.setAttribute("saleGridList", saleGridList);

		setTreeNodesToSession(request, userInfo);

		return mapping.findForward("leftFrame");
	}

	public ActionForward mainFrame(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return mapping.findForward("mainFrame");
	}

	public ActionForward lrFrame(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return mapping.findForward("lrFrame");
	}

	public void setTreeNodesToSession(HttpServletRequest request, UserInfo userInfo) {
		SysModule entity = new SysModule();
		entity.getMap().put("is_admin", userInfo.getIs_admin().intValue() == 1 ? "1" : null);
		entity.getMap().put("user_id", userInfo.getUser_id());

		List<SysModule> sysModuleList = getFacade().getSysModuleService().getSysModuleListForUser(entity);
		Set<SysModule> sysModuleSet = new TreeSet<SysModule>();
		sysModuleSet.addAll(sysModuleList);

		getSysModuleListForModpopedom(sysModuleSet, sysModuleList);

		String treeNodes = StringHelper.getTreeNodesFromSysModuleList(sysModuleSet);
		request.setAttribute("treeNodes", treeNodes);
	}

	// 登录成功后用户节点树的生成(递归调用)
	private void getSysModuleListForModpopedom(Set<SysModule> sysModuleSet, List<SysModule> smList) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("smList", smList);
		List<SysModule> sysModuleList = super.getFacade().getSysModuleService().getSysModuleListForModpopedom(map);

		if (null != sysModuleList && sysModuleList.size() > 0) {
			sysModuleSet.addAll(sysModuleList);

			getSysModuleListForModpopedom(sysModuleSet, sysModuleList);
		}
	}

}
